package com.learning.netease.optimize.frontback.session;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.CompressionCodecs;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import javax.crypto.spec.SecretKeySpec;

/**
 * ClassName: JwtTokenProvider
 * Description:
 * Date: 2019/1/29 15:51 【需求编号】
 *
 * @author Sam Sho
 * @version V1.0.0
 */
public class JwtTokenProvider {

    private final SecretKeySpec key;

    public JwtTokenProvider(String key) {
        this.key = new SecretKeySpec(key.getBytes(), SignatureAlgorithm.HS512.getJcaName());
    }


    /**
     * 生成Token
     *
     * @param claims
     * @return
     */
    public String createToken(Claims claims) {
        return Jwts.builder()
                .setClaims(claims)
                .compressWith(CompressionCodecs.DEFLATE)
                .signWith(SignatureAlgorithm.HS512, key)
                .compact();

    }

    /**
     * 解析Token
     *
     * @param token
     * @return
     */
    public Claims parseToken(String token) {
        return Jwts.parser().setSigningKey(key).parseClaimsJws(token).getBody();
    }


}
